<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        background-color: red;
        opacity: 1;
      }
    </style>
  </head>
  <body>
    <div id="odiv"></div>

    <script>
      const odivEl = document.querySelector("#odiv");
      odivEl.onclick = function () {
        startMove(this, "opacity", "0");
      };

      let timer = null;

      function getStyle(dom, attr) {
        if (window.getComputedStyle) {
          return window.getComputedStyle(dom)[attr];
        } else {
          return dom.currentStyle[attr];
        }
      }

      function startMove(dom, attr, target) {
        clearInterval(timer);
        let iSpeend = null;
        let curTarget = null;
        timer = setInterval(() => {
          curTarget = parseFloat(getStyle(dom, attr)) * 100;
          target = parseFloat(target);
          iSpeend = (target - curTarget) / 7;
          iSpeend = iSpeend > 0 ? Math.ceil(iSpeend) : Math.floor(iSpeend);
          if (curTarget === target) {
            clearInterval(timer);
          } else {
            dom.style[attr] = (curTarget + iSpeend) / 100;
          }
        }, 30);
      }
    </script>
  </body>
</html>
